//
//  ZJEmotionTool.swift
//  Webo
//
//  Created by 曾静 on 16/7/5.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit


let emotionMaxXCol = 7
let emotionMaxXRow = 3
let emotionMaxXCount = emotionMaxXCol * emotionMaxXRow - 1
class ZJEmotionTool: NSObject {

    
    //设置单例创建全局访问点
    static let shareEmotionTools:ZJEmotionTool = ZJEmotionTool()
    
    //懒加载bundle
    
     lazy var emotionBundle:NSBundle = {
        
        return NSBundle(path: NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!)!

    }()
    
    //设置方法,通过传入文件的具体指,获取到plist,解析出数组,转化问模型
    func getEmotions(pathName:String)->[ZJEmotion]{
        
        let array = NSArray(contentsOfFile: emotionBundle.pathForResource(pathName + "info.plist", ofType: nil)!)!
        
        
        //创建可变的数组
        var marr  = [ZJEmotion]()
        //遍历数组字典转化模型
        for dict in array{
            
           let emotion = ZJEmotion(dict: dict as! [String : AnyObject])
            
            emotion.path = pathName
            
            marr.append(emotion)
        }
        
        return marr
        
    }
    //懒加载三维数组
     lazy var allEmotions:[[[ZJEmotion]]] = {
    
       return [
        
        self.changeArrayIntoDoubleArray(self.defaultArr),
        self.changeArrayIntoDoubleArray(self.emojiArr),
        self.changeArrayIntoDoubleArray(self.lxhArr),
    
        ]
    
    }()

    
    //通过懒加载创建三个数组属性
     lazy var defaultArr:[ZJEmotion] = {
    
        return self.getEmotions("default/")
    
    }()
    //通过懒加载创建三个数组属性
    lazy var emojiArr:[ZJEmotion] = {
        
        return self.getEmotions("emoji/")
        
    }()
    //通过懒加载创建三个数组属性
    lazy var lxhArr:[ZJEmotion] = {
        
        return self.getEmotions("lxh/")
        
    }()
    
    //将一位数组转化为二维数组
    func changeArrayIntoDoubleArray(array:[ZJEmotion])->[[ZJEmotion]]{
        
        let page = (array.count + emotionMaxXCount - 1) / emotionMaxXCount
        
        var marr = [[ZJEmotion]]()
        
        for i in 0..<page {
            
            let loc = i * emotionMaxXCount
            
            var len = emotionMaxXCount
            
            if len + loc > array.count {
                
                len = array.count - loc
            }
            
            let arr = (array as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as![ZJEmotion]
            
            marr.append(arr)
            
        }
        return marr
        
    }
 
}
